****Stimson's dyad-ratio algorithm
***Prepare data
clear
use "Trust_trends_rep.dta"

drop if year>2019

cap drop month
cap drop day
cap drop date
gen month=1
gen day=1
gen date=mdy(month, day, year)
format date %td

*Create a variable indicating each country+survey source combination in the dataset
cap drop varname

gen varname=study_all2*10000
drop if varname==.

cap drop value_parl
cap drop value_gov
cap drop value_polpar
cap drop value_leg
cap drop value_police
cap drop value_civil

gen value_parl=trust_parl
gen value_gov=trust_gov
gen value_polpar=trust_polpar
gen value_leg=trust_leg
gen value_police=trust_police
gen value_civil=trust_civil

drop if date==.

*Make identifier variable a string variable and shorten
replace varname=varname/10000
tostring varname,replace
format varname %11s
la var varname "Var"

*Separate by country
cap gen country_tr=subinstr(country, " ", "", .)
replace varname=country_tr+varname
format varname %11s

*In the Stimson's analysis, we follow Stimson's advice and code each variation in question wording within survey projects as separate items (as well as variations in response scales)
*Recode scale and wording variables so that missing observations take the value 0, so they are not dropped from the analysis
cap tostring q_scale_parl, replace
cap tostring q_scale_gov, replace
cap tostring q_scale_polpar, replace
cap tostring q_scale_leg, replace
cap tostring q_scale_civil, replace
cap tostring q_scale_police, replace

la val q_wording_gov .
cap tostring q_wording_gov, replace
cap tostring q_wording_parl, replace
cap tostring q_wording_polpar, replace
cap tostring q_wording_leg, replace
cap tostring q_wording_civil, replace
cap tostring q_wording_police, replace

replace q_scale_parl="0" if q_scale_parl=="."
replace q_scale_gov="0" if q_scale_gov=="."
replace q_scale_polpar="0" if q_scale_polpar=="."
replace q_scale_leg="0" if q_scale_leg=="."
replace q_scale_civil="0" if q_scale_leg=="."
replace q_scale_police="0" if q_scale_leg=="."

replace q_wording_parl="0" if q_wording_parl=="."
replace q_wording_gov="0" if q_wording_gov=="."
replace q_wording_polpar="0" if q_wording_pol=="."
replace q_wording_leg="0" if q_wording_pol=="."
replace q_wording_civil="0" if q_wording_pol=="."
replace q_wording_police="0" if q_wording_pol=="."

*Fill in question wordings for some cases if needed
replace q_wording_polpar="0" if !missing(trust_polpar) & study_all=="Irish National Election Study"
replace q_wording_polpar="1" if !missing(trust_polpar) & study_all=="Norwegian Election Study" & year==1997
replace q_wording_polpar="0" if !missing(trust_polpar) & study_all=="Norwegian Election Study"
replace q_wording_polpar="5" if !missing(trust_polpar) & study_all=="Arab Barometer"
replace q_wording_polpar="6" if !missing(trust_polpar) & study_all=="Arab Barometer" & (year==2010 | year==2011 | year==2012)
replace q_wording_polpar="5" if study_all=="Asian Barometer Survey" & !missing(trust_polpar)
replace q_wording_polpar="4" if study_all=="World Values Survey" & !missing(trust_polpar)

*Move Australia and New Zealand from the WE/NA region to the Asia & Pacific region (if needed)
replace regpol6=1 if country=="Australia" | country=="New Zealand"

*Code democracy classification, whether a country was coded as an electoral or liberal democracy by the VDem RoW measure in a majority of the years in which it's included in our data
cap drop vdem_row_perc
cap drop vdem_row_maj
cap drop vdem_row_bin
recode row_vdem (0 1=0) (2 3=1), gen(vdem_row_bin)
sort country year
by country, sort: egen vdem_row_perc=mean(vdem_row_bin)
recode vdem_row_perc (min/0.5=0) (0.51/1=1), gen(vdem_row_maj)
replace vdem_row_maj=1 if country=="Andorra" | country=="Belize"
save "Int_all_stims_sc.dta", replace

***Separate datasets for each trust variable
**Parliament
clear
use "Int_all_stims_sc.dta"

*Create separate variable identifiers for changes in response scales and question wording
replace varname=varname+q_scale_parl
replace varname=varname+q_wording_parl

rename value_parl value
keep varname date value ncases_parl regpol6 country2 vdem_row_maj year study
drop if value==.
replace value=100*value
replace value=0.1 if value==0

order varname date value ncases_parl regpol6 country2 vdem_row_maj year study
sort varname date
rename ncases_parl ncases

save "Stims_parl_sc.dta",replace
export delimited using "Stims_parl_sc.csv", replace
export delimited using "Bayesian\parl_wording.csv", replace

**Government
clear
use "Int_all_stims_sc.dta"

*Create separate variable identifiers for changes in response scales and question wording
replace varname=varname+q_scale_gov
replace varname=varname+q_wording_gov

rename value_gov value
keep varname date value ncases_gov regpol6 country2 vdem_row_maj year study
drop if value==.
replace value=100*value
replace value=0.1 if value==0

order varname date value ncases_gov regpol6 country2 vdem_row_maj year study
sort varname date
rename ncases_gov ncases

save "Stims_gov_sc.dta",replace
export delimited using "Stims_gov_sc.csv", replace
export delimited using "Bayesian\gov_wording.csv", replace

**Political Parties
clear
use "Int_all_stims_sc.dta"

*Create separate variable identifiers for changes in response scales and question wording
replace varname=varname+q_scale_polpar
replace varname=varname+q_wording_polpar

rename value_polpar value
keep varname date value ncases_polpar regpol6 country2 vdem_row_maj year study
drop if value==.
replace value=100*value
replace value=0.1 if value==0

order varname date value ncases_polpar regpol6 country2 vdem_row_maj year study
sort varname date
rename ncases_polpar ncases

save "Stims_polpar_sc.dta",replace
export delimited using "Stims_polpar_sc.csv", replace
export delimited using "Bayesian\polpar_wording.csv", replace

**Legal system
clear
use "Int_all_stims_sc.dta"

*Create separate variable identifiers for changes in response scales and question wording
replace varname=varname+q_scale_leg
replace varname=varname+q_wording_leg

rename value_leg value
keep varname date value ncases_leg regpol6 country2 vdem_row_maj year study
drop if value==.
replace value=100*value
replace value=0.1 if value==0

order varname date value ncases_leg regpol6 country2 vdem_row_maj year study
sort varname date
rename ncases_leg ncases

save "Stims_leg_sc.dta",replace
export delimited using "Stims_leg_sc.csv", replace
export delimited using "Bayesian\leg_wording.csv", replace

**Civil service
clear
use "Int_all_stims_sc.dta"

*Create separate variable identifiers for changes in response scales and question wording
replace varname=varname+q_scale_civil
replace varname=varname+q_wording_civil

rename value_civil value
keep varname date value ncases_civil regpol6 country2 vdem_row_maj year study
drop if value==.
replace value=100*value
replace value=0.1 if value==0

order varname date value ncases_civil regpol6 country2 vdem_row_maj year study
sort varname date
rename ncases_civil ncases

save "Stims_civil_sc.dta",replace
export delimited using "Stims_civil_sc.csv", replace
export delimited using "Bayesian\civil_wording.csv", replace

**Police
clear
use "Int_all_stims_sc.dta"

*Create separate variable identifiers for changes in response scales and question wording
replace varname=varname+q_scale_police
replace varname=varname+q_wording_police

rename value_police value
keep varname date value ncases_police regpol6 country2 vdem_row_maj year study
drop if value==.
replace value=100*value
replace value=0.1 if value==0

order varname date value ncases_police regpol6 country2 vdem_row_maj year study
sort varname date
rename ncases_police ncases

save "Stims_police_sc.dta",replace
export delimited using "Stims_police_sc.csv", replace
export delimited using "police_wording.csv", replace